package com.base.util;

import java.io.Serializable;

import org.apache.commons.lang.StringUtils;

public class BasePager implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6028445228450955704L;
	
	
	private int currentPage = 0; // 当前页
	private int allCount = 0; // 总行数
	private int pageSize = 10; // 页大小
	private int pageCount = 0; // 总页数
	private int pageOffset = 0; //起始页
	private String orderBy; //排序字段
	private boolean orderDirection;  //true: desc
	
	public int getPageOffset() {
		return pageOffset;
	}
	public void setPageOffset(int pageOffset) {
		this.pageOffset = pageOffset;
	}
	public String getOrderBy() {
		return orderBy;
	}
	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}
	public boolean isOrderDirection() {
		return orderDirection;
	}
	public void setOrderDirection(boolean orderDirection) {
		this.orderDirection = orderDirection;
	}
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public int getAllCount() {
		return allCount;
	}
	public void setAllCount(int allCount) {
		this.allCount = allCount;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getPageCount() {
		return pageCount;
	}
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
	
	//获得分页查询起始页
	public int getPageNum(int currnetPage){
		return currnetPage * this.pageSize;
	}
	
	//获得总页数
	public int getAllPageNum(int allCount){
		return (allCount + this.pageSize -1 )/pageSize;
	}
	
	public String getOrderCondition() {
		String condition = "";
		if (StringUtils.isNotBlank(orderBy)) {
			condition = " order by " + orderBy
					+ (orderDirection ?" desc ":" asc" );
		}
		return condition;
	}
	
	/**
	 * @title 修改-设置PageSize>0时才返回分页查询条件
	 * @author guobingbing
	 * @date 2016年6月13日 下午1:49:42
	 * @description TODO
	 * @return
	 */
	public String getMysqlQueryCondition() {
		String condition = "";
		if(pageSize>0){
			pageOffset=this.getPageNum(this.getCurrentPage());
			condition = " limit " + pageOffset + "," + pageSize;
		}
		return condition;
	}
}
